import {defineStore} from 'pinia';
import {Http, Cache, Helper, Constant, Msg} from 'core';
const store = {
    id: "store",
    state() {
        return {
            storeList: [],
            storeInfo: {},
        }
    },

    getters: {},

    actions: {
        /**
         * 获取我管辖的门店列表
         * @returns {Promise<[]|*>}
         */
        async getList() {
            Msg.loading("加载中...")
            this.storeList = [];
            let {code, data} = await Http.get("/store/getMyStore")
            Msg.loading(false);
            if(code == 200){
                this.storeList = data;
            }
            return this.storeList;
        },

        /**
         * 获取门店信息
         * @param id
         * @returns {Promise<[]|*>}
         */
        async getInfo(id) {
            Msg.loading("加载中...")
            this.storeInfo = [];
            let {code, data} = await Http.get("/store/info/" + id)
            Msg.loading(false);
            if(code == 200){
                this.storeInfo = data;
            }
            return this.storeInfo;
        },

        /**
         * 门店保存
         * @param form
         */
        async save(form, address) {

            if (!this.validate(form)) {
                return;
            }
            // {provinceName: "河南省", cityName: "漯河市", areaName: "临颍县"}
            form.storeShortName = form.storeName;
            address.info = form.storeDetailAddress;
            form.storeAddress = Helper.formatAddress(address);
            Msg.loading("加载中...");
            let {code, data} = await Http.post("/store/save", form);
            Msg.loading(false);

            if (code == 200) {
                if (data) {
                    Msg.success("保存门店成功");
                } else {
                    Msg.error("保存门店失败");
                }
                return data;
            }
            return false;
        },

        /**
         * 门店保存
         * @param form
         */
        async update(form, address) {
            //store_address
            if (!this.validate(form)) {
                return;
            }
            form.storeShortName = form.storeName;
            address.info = form.storeDetailAddress;
            form.storeAddress = Helper.formatAddress(address);
            Msg.loading("加载中...");
            let {code, data} = await Http.post("/store/update", form);
            Msg.loading(false);

            if (code == 200) {
                if (data) {
                    Msg.success("修改门店成功");
                } else {
                    Msg.error("修改门店失败");
                }
                return data;
            }
            return false;
        },

        /**
         * 验证
         * @param form
         */
        validate(form) {
            if (Helper.empty(form.storeName)) {
                Msg.error("请输入门店名称");
                return false;
            }

            if (Helper.empty(form.customerServiceNumber)) {
                Msg.error("请输入售后电话号码")
                return false;
            }

            if (Helper.empty(form.province)) {
                Msg.error("请选择省份")
                return false;
            }

            if (Helper.empty(form.city)) {
                Msg.error("请选择城市")
                return false;
            }

            if (Helper.empty(form.county)) {
                Msg.error("请选择区县")
                return false;
            }

            if (Helper.empty(form.storeDetailAddress)) {
                Msg.error("请输入门店详细地址")
                return false;
            }

            return true;
        }
    }
}

export default defineStore(store)
